<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left" v-loading="sysSwitching"  element-loading-text="系统切换中...">
      <div class="title-container">
        <el-image :src="logo_path" class="logo-img">
          <div slot="placeholder">
            加载中<span class="dot">...</span>
          </div>
        </el-image>
        <span class="title">伟明报表系统</span>
      </div>
      <el-form-item prop="mobile">
        <span class="svg-container svg-container_login">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="mobile"
          v-model="loginForm.mobile"
          placeholder="用户名"
          name="mobile"
          type="text"
          tabindex="1"
          autocomplete="on"
          readonly
          onfocus="this.removeAttribute('readonly');"
        />
      </el-form-item>
      <el-tooltip v-model="capsTooltip" content="大写锁定已开启" placement="right" manual>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            autocomplete="on"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'passwgord' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
      </el-tooltip>
      <div class="pwdsave-view">
        <el-checkbox v-model="savePwd" @change="pwdSaveChange">保存密码</el-checkbox>
        <span class="forgetPwd-Span" @click="forgetPwd">忘记密码</span>
      </div>
      <div style="margin-bottom: 40px" />
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin" @keyup.enter.native="handleLogin">登录</el-button>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { LOGINITEMS } from '@/utils/constants'
import { saveItem, getItem, delItem } from '@/utils/storage'
import {setToken} from "@/utils/auth"

export default {
  name: 'Login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('请输入不少于六个字符的密码'))
      } else {
        callback()
      }
    }
    return {
      logo_path: 'http://wmhb.wm-mis.com/wm_logo.jpg',
      loginForm: {
        mobile: '',
        password: ''
      },
      loginRules: {
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      savePwd: false,
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      redirect: undefined,
      otherQuery: {},
      sysSwitching:false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    let patten = /token=(.*)/ig
    const href = window.location.href
    if (href.search(patten)>-1){
      this.sysSwitching=true
      let acc_token = null
      href.replace(patten,function () {
        acc_token = arguments[1]
      })
      this.$store.commit('user/SET_TOKEN', acc_token)
      setToken(acc_token)
      this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
    }

  },
  mounted() {
    const logonName = getItem(LOGINITEMS.USERNAME)
    const logonPwd = getItem(LOGINITEMS.PASSWORD)
    if (logonName) { this.loginForm.mobile = logonName }
    if (logonPwd) {
      this.loginForm.password = logonPwd
      this.savePwd = true
    }
    if (this.loginForm.mobile === '') {
      this.$refs.mobile.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then((orgs) => {
              this.loading = false
              saveItem(LOGINITEMS.USERNAME, this.loginForm.mobile)
              if (this.savePwd) { saveItem(LOGINITEMS.PASSWORD, this.loginForm.password) }
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    },
    pwdSaveChange(value) {
      if (!value) {
        delItem(LOGINITEMS.PASSWORD)
      }
    },
    forgetPwd() {
      this.$message.info('请联系管理员')
    }
  }
}
</script>

<style lang="scss">
  $bg: #2d3a4b;
  $dark_gray: #889aa4;
  $light_gray: #eee;

  .login-container {
    .el-input {
      display: inline-block;
      height: 40px;
      width: 85%;
      input {
        background: transparent;
        border: 0;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        height: 35px;
        &:-webkit-autofill{
           -webkit-box-shadow: 0 0 0px 1000px #f0f2f5 inset !important;
        }
        &:-webkit-autofill:hover,
        &:-webkit-autofill:focus,
        &:-webkit-autofill:active {
          -webkit-box-shadow: 0 0 0px 1000px #889aa4 inset !important;
          -webkit-text-fill-color: #fff !important;
        }

      }
    }

    .el-form-item {
      height: 43px;
      border: 1px solid $dark_gray;
      border-radius: 5px;
      color: #454545;
    }

    position: fixed;
    height: 100%;
    width: 100%;
    background: #3F5D7E url(~@/assets/login.svg) no-repeat;
    .login-form {
      position: absolute;
      background-color: white;
      border-radius: 10px;
      left: 0;
      right: 0;
      max-width: 520px;
      padding: 35px 35px 15px 35px;
      margin: 120px auto;
    }
    .svg-container {
      padding: 3px 5px 8px 15px;
      vertical-align: middle;
      width: 35px;
      display: inline-block;
      color: $bg;
      &_login {
        font-size: 15px;
      }
    }
    .title-container {
      position: relative;
      margin-bottom: 50px;
      display: flex;
      justify-content: center;
      align-items: center;
      .title {
        padding: 10px;
        font-size: 26px;
        color: $bg;
        text-align: center;
        font-weight: bold;
      }
      .logo-img{
        width: 80px;
        height: 50px;
        box-sizing: border-box;
        box-shadow: 0 2px 4px rgba(0,0,0,0.3);
      }
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
    .thirdparty-button {
      position: absolute;
      right: 35px;
      bottom: 28px;
    }
    .loginButton{
      width:100%;
      height: 45px;
      margin-bottom:30px;
      background-color: #3a8ee6;
      border-color: #3a8ee6;
      color: white;
    }
    .pwdsave-view{
      width: 100%;
      height: 20px;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .forgetPwd-Span{
      cursor: pointer;
      font-size: 14px;
      color:#606266;
    }

  }
</style>
